package idc

import (
	"go.uber.org/zap"
	"wzgames/internal/gdao"
	"wzgames/pkg/logger"
)

type IdDao gdao.DaoSource

var bizQueryById = "select * from id_stamp where bizid=? limit 1"

func (dao *IdDao) GetBizStampById(bizid int64) (*IdSegment, error) {
	stamp := &IdSegment{}
	err := dao.Get(stamp, bizQueryById, bizid)
	return stamp, err
}

var updateBizQuery = "update id_stamp set maxid=maxid+idstep,updated=:updated where bizid=:bizid and maxid=:maxid and idstep=:idstep"

func (dao *IdDao) UpdateBizStamp(idseg *IdSegment) (int64, error) {
	result, err := dao.NamedExec(updateBizQuery, idseg)
	if err != nil {
		logger.Error("err == ", zap.String("error", err.Error()))
		return 0, err
	}
	r, e := result.RowsAffected()
	return r, e
}

var updateBizSectionQuery = "update id_stamp set maxid=maxid+:customstep,updated=:updated where bizid=:bizid and maxid=:maxid and idstep=:idstep"

func (dao *IdDao) UpdateBizSectionStamp(idseg *IdSegment) (int64, error) {
	result, err := dao.NamedExec(updateBizSectionQuery, idseg)
	if err != nil {
		return 0, err
	}
	r, e := result.RowsAffected()
	return r, e
}
